﻿using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.WebControls;
using SJKP.PDFConversion.Client;
using SJKP.PDFConversion.Service;

namespace SJKP.PDFConversion.ADMIN.SJKP.OCR
{
    /// <summary>
    /// Administative page for creating a new service application.
    /// </summary>
    public partial class Create : LayoutsPageBase 
    {
        protected IisWebServiceApplicationPoolSection serviceApplicationPoolSection;

        protected override void OnLoad(EventArgs e)
        {
            DialogMaster master = Page.Master as DialogMaster;
            if (master != null)
            {
                master.OkButton.Enabled = true;
            }
            base.OnLoad(e);
        }

        protected override void OnInit(EventArgs e)
        {
            DialogMaster master = Page.Master as DialogMaster;
            if (master != null)
            {
                master.OkButton.Click += new EventHandler(OkButton_Click);
            }

            base.OnInit(e);
        }

        void OkButton_Click(object sender, EventArgs e)
        {
            if (!Page.IsValid)
                return;
            OCRService service = SPFarm.Local.Services.GetValue<OCRService>();
            if (service == null)
                throw new InvalidOperationException("OCR Service not installed");

            using (SPLongOperation longOperation = new SPLongOperation(this))
            {
                longOperation.Begin();

                string name = txtName.Text.Trim();
                OCRServiceApplication application = CreateServiceApplication(service, name);
                OCRServiceApplicationProxy applicationProxy = new OCRServiceApplicationProxy(name + " Proxy", SPFarm.Local.ServiceProxies.GetValue<OCRServiceProxy>(),
                                               application.Uri);
                applicationProxy.Update();
                applicationProxy.Provision();

                //add to default group
                if (cbDefaultGroup.Checked)
                {
                    SPServiceApplicationProxyGroup defaultGroup = SPServiceApplicationProxyGroup.Default;
                    defaultGroup.Add(applicationProxy);
                    defaultGroup.Update(true);
                }
                SendResponseForPopUI();
            }

        }

        private OCRServiceApplication CreateServiceApplication(OCRService service, string name)
        {
            OCRServiceApplication application = new OCRServiceApplication(name, service, serviceApplicationPoolSection.GetOrCreateApplicationPool());
            application.Update();

            if (application.Status != SPObjectStatus.Online)
                application.Status = SPObjectStatus.Online;
            application.PdfConverterType = dvdList.SelectedValue;
            application.AddServiceEndpoint(string.Empty, SPIisWebServiceBindingType.Http);
            application.AddServiceEndpoint("http", SPIisWebServiceBindingType.Http);
            application.Update(true);
            application.Provision();
            return application;
        }
    }
}
